package com.lehuowujie.lehuomarket.ui.login;

import android.content.Intent;
import android.view.View;

import com.lehuowujie.lehuomarket.R;
import com.lehuowujie.lehuomarket.base.BaseActivity;
import com.lehuowujie.lehuomarket.ui.MainActivity;
import com.lehuowujie.lehuomarket.util.SharedPrefUtil;
import com.lehuowujie.lehuomarket.util.ToastUtil;

/**
 * Created by 王瑞铭 on 2017/1/13.
 */
public class LoginActivity extends BaseActivity implements ILoginView {

    private ILoginPresenter mILoginPresenter;

    @Override
    public int setContentViewRes() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        initToolBar();
        setTitle(R.string.login);
        mILoginPresenter = new ILoginPresenterImpl(this);
    }

    @Override
    public void initEvent() {
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mILoginPresenter.login(LoginActivity.this);
            }
        });
        findViewById(R.id.loginOut).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPrefUtil.getInstance(LoginActivity.this).clearUserLogout();
                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(i);
                finish();
            }
        });
    }

    @Override
    public void showLoadingProgress() {
        showNetDialog();
    }

    @Override
    public void hideLoadingProgress() {
        hideNetDialog();
    }

    @Override
    public void onError(String errorMessage) {
        ToastUtil.showLong(this, errorMessage);
    }

    @Override
    public void onLoginSuccess() {
        SharedPrefUtil.getInstance(this).setLogined(true);
        finish();
    }
}
